package com.ruoyi.framework.aspectj;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampReLogs;
import com.ruoyi.eamp.domain.EampReMan;
import com.ruoyi.eamp.domain.EampSrLog;
import com.ruoyi.eamp.domain.vo.EampReManVO;
import com.ruoyi.eamp.service.IEampReLogsService;
import com.ruoyi.eamp.service.IEampSrLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Aspect
@Component
public class EampReAddLogAspect {

    @Autowired
    private IEampReLogsService eampReLogsService;

    @Autowired
    private IEampSrLogService eampSrLogService;

    @Around("@annotation(com.ruoyi.common.annotation.IEampReAddLog)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 当前登录用户
        Long userId = SecurityUtils.getUserId();
        // 操作时间
        LocalDateTime localDate = LocalDateTime.now();
        // 操作方法
        String menthodName = joinPoint.getSignature().getName();

        Long reId = 0L;
        Long srId = 0L;

        Object result = joinPoint.proceed();

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if(arg instanceof EampReMan){
                reId = ((EampReMan) arg).getReId();
                srId = ((EampReMan) arg).getReSrId();
            }

            if(arg instanceof EampReManVO){
                reId = ((EampReManVO) arg).getEampReMan().getReId();
                srId = ((EampReManVO) arg).getEampReMan().getReSrId();
            }
        }

        /*
        * 在设备记录增加
        * */
        EampReLogs eampReLogs = new EampReLogs(null,userId,localDate,menthodName,reId,1L);
        eampReLogsService.insertEampReLogs(eampReLogs);

        return result;
    }
}
